Skip to content

Method: lambda$valueToRdfNode$0(LangString, String)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena.util;
16:
17: import cz.cvut.kbss.jopa.datatype.xsd.XsdDatatypeMapper;
18: import cz.cvut.kbss.jopa.datatype.xsd.XsdTemporalMapper;
19: import cz.cvut.kbss.ontodriver.model.Assertion;
20: import cz.cvut.kbss.ontodriver.model.LangString;
21: import cz.cvut.kbss.ontodriver.model.Value;
22: import cz.cvut.kbss.ontodriver.util.IdentifierUtils;
23: import org.apache.jena.datatypes.BaseDatatype;
24: import org.apache.jena.datatypes.RDFDatatype;
25: import org.apache.jena.datatypes.TypeMapper;
26: import org.apache.jena.datatypes.xsd.impl.RDFLangString;
27: import org.apache.jena.rdf.model.Literal;
28: import org.apache.jena.rdf.model.RDFNode;
29: import org.apache.jena.rdf.model.ResourceFactory;
30:
31: import java.time.temporal.TemporalAccessor;
32: import java.time.temporal.TemporalAmount;
33: import java.util.Date;
34:
35: /**
36: * Utility methods for working with Jena API.
37: */
38: public class JenaUtils {
39:
40: private JenaUtils() {
41: throw new AssertionError();
42: }
43:
44: /**
45: * Transforms the specified value to an {@link RDFNode}, be it a resource or a literal.
46: *
47: * @param assertion Assertion representing the asserted property
48: * @param value Value to transform
49: * @return Jena RDFNode
50: */
51: public static <T> RDFNode valueToRdfNode(Assertion assertion, Value<T> value) {
52: final T val = value.getValue();
53: if (IdentifierUtils.isResourceIdentifier(val)) {
54: return ResourceFactory.createResource(value.stringValue());
55: } else if (val instanceof LangString) {
56: final LangString langString = (LangString) val;
57: return langString.getLanguage().map(lang -> ResourceFactory.createLangLiteral(langString.getValue(), lang))
58: .orElseGet(() -> ResourceFactory.createTypedLiteral(langString.getValue()));
59: } else if (val instanceof String) {
60: return assertion.hasLanguage() ? ResourceFactory.createLangLiteral((String) val,
61: assertion.getLanguage()) :
62: ResourceFactory.createTypedLiteral(val);
63: } else if (val instanceof cz.cvut.kbss.ontodriver.model.Literal) {
64: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = (cz.cvut.kbss.ontodriver.model.Literal) val;
65: return createLiteral(ontoLiteral);
66: } else if (val instanceof Date) {
67: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((Date) val).toInstant());
68: return createLiteral(ontoLiteral);
69: } else if (val instanceof TemporalAccessor) {
70: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAccessor) val));
71: return createLiteral(ontoLiteral);
72: } else if (val instanceof TemporalAmount) {
73: final cz.cvut.kbss.ontodriver.model.Literal ontoLiteral = XsdTemporalMapper.map(((TemporalAmount) val));
74: return createLiteral(ontoLiteral);
75: } else {
76: return ResourceFactory.createTypedLiteral(val);
77: }
78: }
79:
80: private static Literal createLiteral(cz.cvut.kbss.ontodriver.model.Literal ontoLiteral) {
81: final TypeMapper typeMapper = TypeMapper.getInstance();
82: RDFDatatype datatype = typeMapper.getTypeByName(ontoLiteral.getDatatype());
83: if (datatype == null) {
84: // If the datatype does not exist, register it as a base datatype without any special behavior
85: datatype = new BaseDatatype(ontoLiteral.getDatatype());
86: typeMapper.registerDatatype(datatype);
87: }
88: return ResourceFactory.createTypedLiteral(ontoLiteral.getLexicalForm(), datatype);
89: }
90:
91: public static Object literalToValue(Literal literal) {
92: if (RDFLangString.rdfLangString.equals(literal.getDatatype())) {
93: return new LangString(literal.getString(), literal.getLanguage());
94: }
95: final cz.cvut.kbss.ontodriver.model.Literal lit = cz.cvut.kbss.ontodriver.model.Literal.from(
96: literal.getLexicalForm(), literal.getDatatypeURI());
97: return XsdDatatypeMapper.getInstance().map(lit).orElse(lit);
98: }
99: }